<template>
	<view class="count">
		<view class="topTong f f-a-c f-j-b">
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=0}">
				01
			</view>
			<view class="xian">

			</view>
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=1}">
				02
			</view>
			<view class="xian">

			</view>
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=2}">
				03
			</view>
			<view class="xian">

			</view>
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=3}">
				04
			</view>
			<view class="xian">

			</view>
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=4}">
				05
			</view>
			<view class="xian">

			</view>
			<view class="itemZi f f-a-c f-j-c" :class="{active:ind>=5}">
				06
			</view>
		</view>
		<!-- 这个是题目 -->
		<view class="gapFilling">
			<view class="itemFill">
				<view class="topic">
					{{list[ind].subject}}
				</view>
				<textarea name="" v-model="value" id="" placeholder="请输入答题内容" class="textare p-20" cols="30"
					rows="10"></textarea>
			</view>
		</view>
		<!-- 图片上传 -->
		<view class="topBod b-f b-r-15 m-b-10">
			<view class="cot m-b-20 f f-a-c">
				上传图片 <view class="duo m-l-10">
					(最多9张)
				</view>
			</view>
			<view class="pingText">
				<u-upload ref="uUpload" :show-progress="false" @on-remove="onRemove" upload-text="点击上传" max-count="9"
					:form-data="fromData" width="160" @on-success="succress" :before-upload="beforeUpload" height="160"
					:action="url"></u-upload>
			</view>
		</view>
		<!-- 上传音频 -->
		<!-- 	<view class="topBod b-f b-r-15 m-b-10">
			<view class="cot m-b-20 f f-a-c">
				上传音频 <view class="duo m-l-10">
					(最多1个)
				</view>
			</view>
			<view class="pingText">
				<image src="/static/unpload.png" @click="uploadSuccess" v-if="!audio" class="audioImg" mode=""></image>
				<image src="/static/audio.png" v-else class="audioImg" mode=""></image>
			</view>
		</view> -->
		<!-- 	<view class="audio p-20">
			<image src="/static/unpload.png" class="audioImg" mode=""></image>
			<image src="/static/audio.png" class="audioImg" mode=""></image>
			<view @click="uploadSuccess" class="btn-content">点击上传音频</view>
		</view> -->
		<!-- 下面的上传视频 -->
		<view class="viode">
			<view class="unload">
				上传最近孩子看书视频
			</view>
			<view class="">
				<view class="">
					<image @click="didPressChooesImage" v-if="!vioid" src="/static/unpload.png" class="unlod" mode="">
					</image>
					<video v-else class="coolViold" :enable-danmu="false" :show-fullscreen-btn="false"
						:show-center-play-btn="false" :danmu-btn="false" :controls="false" :src="vioid"></video>
				</view>
				<view class="dange">
					（单个视频，不超过30Mb）
				</view>
			</view>
		</view>
		<!-- 下方的提交按钮 -->
		<view @click="submig" class="subMig">
			提交
		</view>
		<u-mask :show="shows" @click="show = false">
			<view class="tan p-20 p-t-40">
				<image src="/static/cha.png" @click="shows=false" class="cha" mode=""></image>
				<view class="points m-b-40">
					本次答题得分
				</view>
				<view class="tubiao">
					恭喜完成通关，本次获得 <text>{{scor}}</text>积分
				</view>
				<view class="botBtn">
					<button open-type="share" class="btns" :plain="true">
						<view @click="share" class="fenxiang">
							分享
						</view>
					</button>
					<view @click="next" v-if="ind!=5" class="next">
						下一关
					</view>
				</view>
			</view>
		</u-mask>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				fromData: {
					token: uni.getStorageSync('upload_token'),
					key: ''
				},
				audio: '',
				shows: false,
				vioid: '',
				ind: 0,
				value: '',
				id: '',
				list: [],
				url: uni.getStorageSync('upload_domain'),
				token: uni.getStorageSync('upload_token'),
				domain: uni.getStorageSync('domain'),
				time: 0,
				timeInt: null,
				scor: 0,
				img: '',
				imgList: [],
			}
		},
		onShareAppMessage() {

		},
		onShareTimeline() {

		},
		// 监听
		watch: {
			ind(newVal, oldVal) {}
		},
		onShow() {
			this.fromData.key = this.genKey()
			uni.hideLoading()
		},
		onLoad(options) {
			this.id = options.id
			this.initList()
			this.timeInt = setInterval(() => {
				this.time++
			}, 1000)
		},
		methods: {
			// 上传音频
			uploadSuccess(file) {

				uni.chooseMessageFile({
					count: 1, //默认100
					extension: ['.m4a', '.mp3'], //根据文件拓展名过滤，每一项都不能是空字符串。默认不过滤。
					success: (res) => {
						console.log(res, '55555555');

						let tempFilePaths = res.tempFiles;
						uni.uploadFile({
							url: this.url,
							filePath: tempFilePaths[0].path,
							name: 'file',
							formData: {
								"token": this.token, //接口需要的参数
								"key": `MP3/${+new Date()+'/'+this.randomString()}.mp3`
							},
							// header: {
							// 	'Authorization': "Bearer " + JSON.parse(uni.getStorageSync(
							// 		'USERS_KEY')).access_token,
							// },
							success: (ress) => {
								let n = JSON.parse(ress.data).key
								console.log(n, '121111');
								this.audio = this.domain + '/' + n

								// let filePath = res.tempFilePath
								// uni.openDocument({
								// 	filePath: filePath,
								// 	showMenu: true,
								// 	success: function(res) {}
								// })
							}
						})
					}
				});

				// }
				// 在这里可以处理上传成功后的逻辑
			},
			async initList() {
				this.ind = 0
				let n = await this.$api.ExaminationBook({
					id: this.id
				})
				this.list = n.data.levels
				this.list.forEach(item => {
					if (item.is_submit) {
						this.ind++
					}
				})
				if (this.ind == 6) {
					this.ind = 5
				}
			},
			// 点击下一关
			next() {
				if (this.ind == 5) {
					this.shows = false
					return this.$msg('已经是最后一题')
				}
				this.value = ''
				this.initList()
				this.time = 0
				this.vioid = ''
				this.value = ''
				this.timeInt = setInterval(() => {
					this.time++
				}, 1000)
				this.shows = false

			},
			// 点击分享
			share() {
				uni.share({
					title: '分享',
					path: '/pages/index/index',
					success: function(res) {
						console.log("success:", res);
					},
					fail: function(err) {
						console.log("fail:", err);
					}
				})

			},
			// 点击提交
			async submig() {
				if (!this.value.length && !this.imgList.length && !this.vioid.length) return this.$msg('请答题')
				// if (!this.vioid.length) return this.$msg('请上传视频')
				if (this.ind == 6) {
					return this.$msg('已经答完题目')
				}
				clearInterval(this.timeInt)
				let n = await this.$api.SubmitLevel({
					content: this.value,
					duration: this.time,
					audio: this.audio,
					image: this.imgList.join(','),
					video: this.vioid
				}, this.id, this.list[this.ind].id)
				this.scor = this.list[this.ind].score
				this.shows = true
				if (n.status) {
					this.value = ''
					this.time = ''
					this.audio = ''
					this.imgList = []
					this.vioid = ''
					this.$refs.uUpload.clear()
				}
			},
			//上传视频
			onRemove(index, lists) {
				this.imgList = []
				lists.forEach(item => {
					this.imgList.push(this.domain + '/' + item.response.key)
				})
			},
			succress(data, index, lists, name) {
				uni.hideLoading()
				console.log(data, index, lists, name, '999999999');
				this.imgList.push(this.domain + '/' + data.key)
			},

			genKey() {
				uni.showLoading({
					title: "加载中"
				})
				let n = `img/${+new Date()+'/'+this.randomString()}.png`
				console.log(n);
				return n
			},
			beforeUpload(index, list) {
				console.log(index, list, '000');
				// this.imgList = list.filter(item => {
				// 	return this.domain + '/' + item.response.key
				// })
				this.fromData.key = this.genKey()
				console.log(this.fromData, '8888888');
				return true
			},
			randomString() {
				let str = 'ABCDEFGHIJKIMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
				let num = '';
				for (let i = 0; i < 16; i++) {
					num += str.charAt(Math.floor(Math.random() * str.length));
				}
				console.log(num, '000000000000');
				return num;
			},
			didPressChooesImage() {
				// console.log('11111');
				// console.log();
				// return
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: (res) => {
						// console.log(res, '10000000000000000');
						// this.isLoading = true;
						uni.showLoading({
							title: "上传中..."
						});
						uni.uploadFile({
							url: uni.getStorageSync('upload_domain'), // 上传地址
							filePath: res.tempFilePath, // 要上传的文件路径
							name: 'file', // 上传文件对应的 key 值
							method: 'POST',
							formData: {
								key: `${this.$u.timeFormat(new Date(), 'yyyy-mm-dd')}/${+new Date()}.mp4`,
								token: uni.getStorageSync('upload_token'),
							},
							success: (uploadRes) => {
								// 上传成功处理逻辑
								console.log(uploadRes,
									'0000000000000000000000000000000000000000000000');
								uni.hideLoading();
								this.$msg('上传成功')
								let url = JSON.parse(uploadRes.data).key
								this.vioid = uni.getStorageSync('domain') + '/' + url

							},
							fail: function(error) {
								// 上传失败处理逻辑
								console.log(error,
									'1111111111111111111111111111111111111111111111111111111111'
								);
								uni.hideLoading();
								this.$msg('上传失败')
							},
							complete: function() {}
						});
					}
				})

			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.audioImg {
		width: 150rpx;
		height: 150rpx;
	}


	.upload-btn {
		width: 200rpx;
		height: 100rpx;
		background-color: #f0f0f0;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 5rpx;
	}

	.btn-content {
		color: #333;
	}

	.btns {
		border: none;
		padding: 0%;
		margin: 0;
		margin-right: 5%;
	}

	.topBod {
		width: 96%;
		margin: 0 auto;

		.cot {
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			color: #333333;
		}

		.inp {
			height: 150rpx;
			width: 100%;
		}
	}

	.duo {
		font-size: 24rpx;
	}

	.tan {
		position: relative;
		width: 70%;
		height: 418rpx;
		background: #ffffff;
		border-radius: 16rpx;
		margin-top: 60%;
		margin-left: 50%;
		transform: translate(-50%, -0%);
		box-sizing: border-box;

		.botBtn {
			position: absolute;
			bottom: 30rpx;
			width: 93%;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;

			.next {
				width: 200rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: #009d85;
				border-radius: 40rpx;
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: center;
				color: #ffffff;
			}

			.fenxiang {
				width: 200rpx;
				height: 80rpx;
				line-height: 80rpx;
				margin-right: 5%;
				border: 2rpx solid #009d85;
				border-radius: 42rpx;
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: center;
				color: #009d85;
			}
		}

		.tubiao {
			text-align: center;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC-Regular;
			font-weight: 400;
			text-align: center;
			color: #333333;

			text {
				color: #009d85;
			}
		}

		.points {
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			text-align: center;
			color: #000000;
		}

		.cha {
			width: 42rpx;
			height: 42rpx;
			position: absolute;
			top: -60rpx;
			right: 5rpx;
		}
	}

	.subMig {
		width: 90%;
		height: 88rpx;
		line-height: 85rpx;
		background: #009d85;
		border-radius: 44rpx;
		position: fixed;
		bottom: 30rpx;
		left: 5%;
		font-size: 30rpx;
		font-family: PingFang SC, PingFang SC-Bold;
		font-weight: 700;
		text-align: center;
		color: #ffffff;
	}

	.dange {
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #999999;
		margin-top: 20rpx;
	}

	video {
		width: 160rpx;
		height: 160rpx;
		margin-top: 30rpx;
	}

	.viode {
		margin-top: 50rpx;
		padding: 20rpx;

		.unlod {
			width: 160rpx;
			height: 160rpx;
			margin-top: 30rpx;
		}

		.unload {
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			text-align: left;
			color: #333333;
		}
	}

	.gapFilling {
		padding: 20rpx;
		margin-top: 50rpx;

		.topic {
			text-indent: 2em;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			color: #333333;
		}

		.textare {
			width: 690rpx;
			height: 200rpx;
			background: #fbfbfb;
			border-radius: 16rpx;
			margin-top: 20rpx;
		}
	}

	.count {
		width: 100%;
		height: 100vh;
		padding-top: 85rpx;
		overflow-y: auto;
	}

	.topTong {
		position: fixed;
		top: 20rpx;
		left: 0;
		width: 100%;
		height: 65rpx;
		padding: 0 20rpx;

		.xian {
			width: 34rpx;
			height: 0rpx;
			border: 2rpx solid #009d85;
		}

		.itemZi {
			width: 64rpx;
			height: 64rpx;
			background: #66c4b6;
			border-radius: 50%;
			font-size: 32rpx;
			font-family: DIN, DIN-Bold;
			font-weight: 700;
			text-align: center;
			color: #ffffff;
		}

		.active {
			background: #009d85;
		}
	}
</style>